package main

import (
	"fmt"
	"strings"
)

func main() {
	S := "a-a-a-a-"
	K := 1
	fmt.Println(licenseKeyFormatting(S, K))
}

//#link: https://leetcode-cn.com/problems/license-key-formatting/
func licenseKeyFormatting(s string, k int) string {
	characters := make([]byte, 0)
	for i := 0; i < len(s); i++ {
		if s[i] != '-' {
			characters = append(characters, s[i])
		}
	}
	if len(characters) == 0 {
		return ""
	}
	pre := len(characters) / k
	firstPart := len(characters) % k
	result := make([]byte, 0)
	if firstPart != 0 {
		result = append(result, characters[:firstPart]...)
		result = append(result, '-')
	}
	nextPart := firstPart
	for i := 1; i <= pre; i++ {
		endIndex := nextPart + k
		if endIndex >= len(characters) {
			endIndex = len(characters)
		}
		result = append(result, characters[nextPart:endIndex]...)
		result = append(result, '-')
		nextPart = endIndex
	}

	result = result[:len(result)-1]
	return strings.ToUpper(string(result))

}
